home *** CD-ROM | disk | FTP | other *** search
/ JCSM Shareware Collection 1996 September / JCSM Shareware Collection (JCS Distribution) (September 1996).ISO / prgtools / 11730a01.ziv / VTOOLS21.EXE / EXAMPLE.C < prev    next >
Text File  |  1994-02-26  |  4KB  |  222 lines

  1. #include <stdlib.h>;
  2. #include <stdio.h>;
  3. #include <dos.h>;
  4. #include <stdarg.h>
  5. #include "\gt\mcgalib.h";
  6.  
  7. #define ulong unsigned long
  8. #define uchar unsigned char
  9. #define VGA256 19
  10. #define TEXTMODE 3
  11.  
  12. FILE *FP;
  13. int *AUTOMATON;              /* Pointer on buffer for automaton */
  14. char *BLOCKSAVE;             /* Pointer on buffer for screen block */
  15. int AUTMAX;                  /* Number of entries in automaton */
  16. int SPRMAX;                  /* Number of sprites in table */
  17.  
  18. void changext(char *name,char *ext);
  19. void outtextf(int x,int y,char *fmt, ... );
  20. int bload(char *fname,char *adr,int siz);
  21. int getcode(int n);
  22. void putpic(int num,int x,int y);
  23. void putscenery(int num,int x,int y);
  24. int autload(char *name);
  25. int sprload(char *name);
  26. int spralloc();
  27.  
  28. /* Function to add or replace extension of filenames */
  29.  
  30. void changext(char *name,char *ext)
  31. {
  32. int l;
  33.  
  34. l=min( strcspn(name,".") , 8);
  35. *(name+l)=0;
  36. strcat(name,ext);
  37. }
  38.  
  39. /* Displays a formatted text string */
  40.  
  41. void outtextf(int x,int y,char *fmt, ... )
  42. {
  43.  char STRBUF19[160];
  44.  
  45.  va_list  argptr;
  46.  va_start( argptr, format );
  47.  vsprintf(STRBUF19, fmt, argptr );
  48.  outtext(x,y,STRBUF19);
  49.  va_end( argptr );
  50. }
  51.  
  52. /* Function to read automaton */
  53.  
  54. int autread(int i)
  55. {
  56. return ( *(AUTOMATON+i));
  57. }
  58.  
  59. /* Function to get sprite type code */
  60.  
  61. int getcode(int n)
  62. {
  63. char *adr=getspadr(n);  return((int)*(adr+4));
  64. }
  65.  
  66. /* Puts a sprite on screen with OVERWRITING mode */
  67.  
  68. void overpic(int num,int x,int y)
  69. {
  70. if(num)
  71.  if(num <= SPRMAX)
  72.    sprite256(x,y,getspadr(num));
  73. }
  74.  
  75. /* Puts a sprite on screen with COPY mode */
  76.  
  77. void putscenery(int num,int x,int y)
  78. {
  79. if(num)
  80.  if(num <= SPRMAX)
  81.    csprite256(x,y,getspadr(num));
  82. }
  83.  
  84. /* Error message for loading function */
  85.  
  86. int puterr(char *name)
  87. {
  88. printf("File no found %s",name);
  89. exit(0);
  90. }
  91.  
  92. /* Function for loading any binary file */
  93.  
  94. int bload(char *fname,char *adr,int siz)
  95. {
  96.  if((FP=fopen(fname,"rb"))==0L) puterr(fname);
  97.  fread((char *)adr,siz,1,FP);
  98.  fclose(FP);
  99.  return(1);
  100. }
  101.  
  102. /* Function for loading an automaton */
  103.  
  104. int autload(char *aname)
  105. {
  106. int ret;
  107. char name[80];
  108.  
  109. strcpy(name,aname);changext(name,".AUT");
  110. ret=bload(name,(char *) AUTOMATON,2048);
  111. AUTMAX=*AUTOMATON;
  112.  
  113. return(ret);
  114. }
  115.  
  116.  
  117. /* Function for loading a file of sprites into memory.
  118.    This unregistered version allows only 64Kb data!     */
  119.  
  120. int sprload(char *spname)
  121. {
  122. int i,res;
  123. ulong lvar;
  124. char name[80];
  125.  
  126. strcpy(name,spname);changext(name,".spr");
  127.  
  128. res=bload(name,(char *) SPVECTOR,65520);
  129. if(!res) return(0);
  130.  
  131. SPRMAX=(int) *SPVECTOR;
  132.  
  133. lvar =  (ulong) SPVECTOR + SPRMAX * 4 + 8;
  134. SPCODE = (char *) lvar;
  135.  
  136. /* Convert relative addresses to absolute ones */
  137.  
  138. for(i = 1; i <= SPRMAX; i++)
  139. {
  140.  lvar=  *(SPVECTOR+i) + (ulong) SPCODE;
  141.  *(SPVECTOR+i)= lvar;
  142. }
  143.  
  144. return(1);
  145. }
  146.  
  147. /* Allocate memory for all buffers */
  148.  
  149. int spralloc()
  150. {
  151. AUTOMATON = (int *)   malloc(2048);
  152. BLOCKSAVE = (char *)  malloc(6406);
  153. SPVECTOR  = (ulong *) malloc(65520);
  154. if(!SPVECTOR) { printf("Alloc error..."); exit(0);}
  155. }
  156.  
  157. void main()
  158. {
  159. int i,j,num,x,y,y2,yold;
  160.  
  161. /* First, allocation of memory buffer */
  162.  
  163. spralloc();
  164.  
  165. /* Loading sprites  */
  166.  
  167. printf("Loading sprites...");
  168. sprload("demo2");
  169.  
  170. /* loading automaton */
  171.  
  172. printf("Loading automaton...");
  173. autload("demo2");
  174.  
  175.  /* Setting VGA 256 colors screen */
  176.  
  177. selectscreen(VGA256);
  178.  
  179. /* Setting a full screen window */
  180.  
  181. window19(0,0,319,199);
  182.  
  183. /* Drawing the scenery */
  184.  
  185. for(y=0;y<2;y++)
  186.  for(x=0;x<4;x++)
  187.   putscenery(9,x*80,y*80+20);
  188.  
  189. /* Now, setting window's limits left, top, right, bottom */
  190.  
  191.  window19(40,20,280,180);
  192.  
  193. /* Animation example */
  194.  
  195. i=1;
  196. x=140;
  197. y=5; yold = y;
  198. getblock(x,yold,x+31,y+49,BLOCKSAVE);    /* Saving background */
  199.  
  200. while(y < 180)
  201. {
  202.  num=autread(i);
  203.  i++;
  204.  if(i > AUTMAX) i=1;
  205.  
  206.  putblock(x,yold,BLOCKSAVE);          /* Restoring background */
  207.  getblock(x,y,x+31,y+49,BLOCKSAVE);   /* Saving new area */
  208.  sprite256(x,y,getspadr(num));        /* Sprite display */
  209.  
  210.  if(kbhit()) goto finish;
  211.  delay(40);        /* to slow down and set all computers same speed */
  212.  yold = y;
  213.  y++;
  214. }
  215.  
  216. /* Restores DOS text screen and exits */
  217.  
  218. finish:
  219. selectscreen(TEXTMODE);
  220. exit(0);
  221. }
  222.